<template>
  <div>
    <UserAuthorizationAllocation
      ref="UserAuthorizationAllocationRef"
      @refresh="fetchTreeData"
    />
    <a-card class="general-card" title="可用组织授权">
      <a-table row-key="id" :loading="loading" :data="renderData">
        <template #columns>
          <a-table-column title="组织名称" data-index="name" />
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            data-index="operations"
            :width="180"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record)"
              >
                用户授权
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import { Menu } from '@/api/menu';
  import { selectRootTreeOrganization } from '@/api/organization';
  import { TableData } from '@arco-design/web-vue';
  import UserAuthorizationAllocation from './user-authorization-allocation.vue';

  const { loading, setLoading } = useLoading();
  const renderData = ref<TableData[]>([]);

  const UserAuthorizationAllocationRef = ref();

  const fetchTreeData = async () => {
    setLoading(true);
    try {
      const { data } = await selectRootTreeOrganization();
      renderData.value = data as TableData[];
    } catch (error) {
      // err
    } finally {
      setLoading(false);
    }
  };
  fetchTreeData();
  const handleClickToSave = (obj: Menu) => {
    UserAuthorizationAllocationRef.value.handleChangeVisible(obj);
  };
</script>

<script lang="ts">
  export default {
    name: 'MenuList',
  };
</script>

<style scoped lang="less">
  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
  :deep(.arco-table .arco-table-td) {
    cursor: pointer;
  }
</style>
